﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;

namespace SweetSurvey.Tests.RepositoryTests
{
    /// <summary>
    ///This is a test class for ParticipatorRepositoryTest and is intended
    ///to contain all ParticipatorRepositoryTest Unit Tests
    ///</summary>
    [TestClass]
    public class ParticipatorRepositoryTest : RepositoryTestBase
    {
        #region Create Sample Data

        public static string CreateSampleParticipator()
        {
            return (new ParticipatorRepository()).Create(new Participator()
            {
                Date = DateTime.Now
            });
        }

        #endregion

        /// <summary>
        ///A test for Create
        ///</summary>
        [TestMethod]
        public void CreateTest()
        {
            // act
            var participatorId = CreateSampleParticipator();

            // assert
            Assert.IsFalse(string.IsNullOrEmpty(participatorId));
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod]
        public void DeleteTest()
        {
            // prepare
            var id = CreateSampleParticipator();
            var repository = new ParticipatorRepository();
            
            // act
            repository.Delete(id);

            // assert
            try { repository.Get(id); Assert.Fail(); }
            catch (Exception) { }
        }

        /// <summary>
        ///A test for GetParticipatorCountBySurveyId
        ///</summary>
        [TestMethod]
        public void GetParticipatorCountBySurveyIdTest()
        {
            // variables
            var participatorCount = 10;
            var answerCount = 25;

            // prepare
            var participatorIds = new List<string>();
            for (var i = 0; i < participatorCount; i++) { participatorIds.Add(CreateSampleParticipator()); }
            var surveyId = SurveyRepositoryTest.CreateSampleSurvey();
            var questionIds = QuestionRepositoryTest.CreateSampleQuestions(surveyId);
            foreach (var questionId in questionIds)
            {
                var optionIds = OptionRepositoryTest.CreateSampleOptions(questionId);
                foreach (var optionId in optionIds)
                {
                    for (var i = 0; i < answerCount; i++)
                    {
                        AnswerRepositoryTest.CreateSampleAnswer(
                            optionId, participatorIds[i % participatorCount]);
                    }
                }
            }
            var r = new ParticipatorRepository();

            // act
            var actualParticipatorCount = r.GetParticipatorCountBySurveyId(surveyId);

            // assert
            Assert.AreEqual(participatorCount, actualParticipatorCount);
        }
    }
}
